<template>
  <div class="app-container">
    <div class="item2">
      <div class="header">
        <div class="title1">
          <i class="el-icon-data-analysis"></i><span>agv待执行任务查询</span>
        </div>
        <div class="backB">
          <img src="@/assets/images/back.png" alt="" @click="goBack" />
        </div>
      </div>
    </div>
    <div class="formCenter">
      <el-form
        :model="pTqueryParams"
        size="large"
        :inline="true"
        ref="pTqueryForm"
        :rules="pTformRules"
        label-width="95px"
      >
        <el-form-item label="W库位:" prop="originalArea">
          <el-input
            v-model="pTqueryParams.originalArea"
            placeholder="请输入W库位"
            clearable
            @keyup.enter.native="pThandleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="medium"
            @click="pThandleQuery"
            >搜索</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-search"
            size="medium"
            @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="pTtableData"
      border
      height="680"
      v-loading="pTloading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      :header-cell-style="{ background: '#1890ff', color: '#ffffff' }"
    >
      <!-- <el-table-column align="center" prop="sourceArea" label="巷道">
              </el-table-column> -->
      <el-table-column align="center" prop="containerNo" label="托盘号">
      </el-table-column>
      <el-table-column align="center" prop="originalArea" label="W库位">
      </el-table-column>
      <el-table-column align="center" prop="status" label="状态">
      </el-table-column>
      <el-table-column align="center" prop="workType" label="任务类型">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.task_type"
            :value="scope.row.workType"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="upperSystemTaskId"
        label="上游任务id"
      >
      </el-table-column>
      <el-table-column align="center" prop="extend1" label="站台">
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="任务生成时间">
      </el-table-column>
      <el-table-column
        align="center"
        prop="priority"
        label="优先级"
        width="230"
      >
        <template slot-scope="scope">
          <!-- <el-select v-model="scope.row.priorty" placeholder="请选择">
                    <el-option
                      v-for="item in priortyOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select> -->
          <el-input
            v-model="scope.row.priority"
            placeholder="请输入优先级"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改优先级</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div>
      <pagination
        v-show="pTtotal > 0"
        :total="pTtotal"
        :page.sync="pTqueryParams.pageNum"
        :limit.sync="pTqueryParams.pageSize"
        @pagination="pThandleQuery"
      />
    </div>
    <div class="footer">
      <el-dialog
        title="操作验证"
        :visible.sync="centerDialogVisible"
        width="22%"
        left
        :close-on-click-modal="false"
      >
        <el-form
          :model="passForm"
          status-icon
          :rules="passrules"
          ref="passForm"
          label-width="60px"
          class="demo-ruleForm"
        >
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="passForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit('passForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getcheckPassword,
  getagvwaitList,
  updateAgvwait,
} from "@/api/subSystem/continer";
import { getPriority } from "@/api/multiPass";
export default {
  dicts: ["work_type", "bill_type", "status_type","task_type"],
  data() {
    var passwordError = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空"));
      } else {
        getcheckPassword(value).then((res) => {
          this.priorityNum = res.msg;
          if (res.msg == "admin") {
            callback();
          } else if (res.msg == "supper") {
            callback();
          } else if (res.msg == "false") {
            callback(new Error("密码错误"));
          }
        });
      }
    };
    return {
      // 托盘待执行任务查询
      // 优先级数值
      priorityNum: null,
      // WMS任务组
      taskid: null,
      // 订单类型
      tasktype: null,
      // 托盘码
      palletid: null,
      // 优先级
      taskgrade: null,
      priortyOptions: [
        {
          value: "A",
          label: "A",
        },
        {
          value: "B",
          label: "B",
        },
      ],
      pTtotal: 0,
      pTtableData: [],
      pTloading: false,
      pTqueryParams: {
        pageNum: 1,
        pageSize: 10,
        originalArea: null,
      },
      pTformRules: {
        originalArea: [
          { required: false, message: "请输入W库位", trigger: "blur" },
        ],
      },
      // 密码验证
      centerDialogVisible: false,
      passForm: {
        pass: null,
      },
      passrules: {
        pass: [{ validator: passwordError, trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.pThandleQuery();
  },
  methods: {
    // 重置
    resetQuery() {
      this.pTqueryParams.originalArea = null;
      getagvwaitList(this.pTqueryParams).then((res) => {
        this.pTtableData = res.rows;
        this.pTtotal = res.total;
        this.pTloading = false;
      });
    },
    // 托盘待执行任务查询
    pThandleQuery() {
      this.$refs.pTqueryForm.validate((valid) => {
        if (valid) {
          this.pTloading = true;
          getagvwaitList(this.pTqueryParams).then((res) => {
            this.pTtableData = res.rows;
            this.pTtotal = res.total;
            this.pTloading = false;
          });
        }
      });
    },
    // 密码验证
    handleUpdate(row) {
      console.log("priorty", row.priority);
      // WMS任务组
      this.taskid = row.upperSystemTaskId;
      //订单类型
      if (row.billType == "整件任务") {
        this.tasktype = "38";
      } else if (row.billType == "整托任务") {
        this.tasktype = "39";
      } else if (row.billType == "盘点任务") {
        this.tasktype = "40";
      }
      // 托盘码
      this.palletid = row.containerNo;
      // 优先级
      this.taskgrade = row.priority;
      this.centerDialogVisible = true;
      this.passForm.pass = null;
      this.thresholdvalue = 1;
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.priorityNum == "admin") {
            if (this.taskgrade >= 10) {
              console.log("admin");
              const randomMsgld = Math.floor(
                Math.random() * 1000000
              ).toString();
              updateAgvwait({
                upperSystemTaskId: this.taskid,
                priority: this.taskgrade,
              }).then((res) => {
                // console.log(res);
                this.pThandleQuery();
                this.activeName = this.currentTabName;
                this.$modal.msgSuccess(res.msg);
              });
              this.centerDialogVisible = false;
            } else {
              this.$modal.msgError("普通用户可修改的优先级值必须大于等于10");
            }
          }
          if (this.priorityNum == "supper") {
            console.log("supper");
            const randomMsgld = Math.floor(Math.random() * 1000000).toString();
            updateAgvwait({
              upperSystemTaskId: this.taskid,
              priority: this.taskgrade,
            }).then((res) => {
              // console.log(res);
              this.pThandleQuery();
              this.activeName = this.currentTabName;
              this.$modal.msgSuccess(res.msg);
            });
            this.centerDialogVisible = false;
          }
        }
      });
    },
    goBack() {
      this.$router.push("/subSystem1");
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  .el-icon-data-analysis::before {
    color: #00d9ff;
    margin-right: 8px;
  }
  .title1 {
    font-size: 16px;
    // margin-bottom: 15px;
    color: #808080;
    width: 200px;
    padding: 5px;
    border-bottom: 1px dotted #8ddfff;
  }
  .header {
    margin-top: -10px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 220px;
      margin-right: 20px;
    }

    .backB {
      cursor: pointer;

      img {
        width: 20%;
      }
    }
  }
  .formCenter {
    height: 62px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 46px 0 27px 120px;
  }
  .footer {
    margin-top: 20px;
    width: 100%;
    min-height: 200px;
  }
  ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
    width: 14px; // 横向滚动条
    height: 14px; // 纵向滚动条 必写
  }
}
</style>